package com.sne.airways.system.dao.impl;

import com.sne.airways.basecore.dao.impl.BaseDAOHibernate;
import com.sne.airways.system.dao.OperationDAO;
import com.sne.airways.system.pojo.TdOperation;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: skyline
 * Date: 12-2-13
 * Time: 下午3:29
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class OperationDAOHibernate extends BaseDAOHibernate implements OperationDAO {

    public Set<TdOperation> queryPrivateChildTree(String userNo,String operationId,String status) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select op from TdOperation op  ");
        sb.append(" left join op.roles rop ");
        sb.append(" left join rop.role r ");
        sb.append(" left join r.users ur ");
        sb.append(" left join ur.user u ");
        sb.append(" where 1 =1 ");
        if(StringUtils.isNotBlank(userNo)){
            sb.append(" and u.userNo = :userNo ");
        }
        if(StringUtils.isNotBlank(operationId)){
            sb.append(" and op.parent = :operationId ");
        }
        if(StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        sb.append(" order by op.menuIndex ASC ");
        Query query=session.createQuery(sb.toString());
        if(StringUtils.isNotBlank(userNo)){
            query.setString("userNo",userNo);
        }
        if(StringUtils.isNotBlank(operationId)){
            query.setString("operationId",operationId);
        }
        if(StringUtils.isNotBlank(status)){
            query.setString("status",status);
        }
        return new HashSet<TdOperation>(query.list());
    }

    /**
     * 根据资菜单ID 返回所有菜单列表
     *
     * @param operationId
     * @param status
     * @return
     */
    @Override
    public Set<TdOperation> queryChildTree(String operationId, String status) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select op from TdOperation op where 1=1 ");
        if(StringUtils.isNotBlank(operationId)){
            sb.append(" and op.parent = :operationId ");
        }
        if(StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        sb.append(" order by op.menuIndex ASC ");
        Query query=session.createQuery(sb.toString());
        if(StringUtils.isNotBlank(operationId)){
            query.setString("operationId",operationId);
        }
        if(StringUtils.isNotBlank(status)){
            query.setString("status",status);
        }
        return new HashSet<TdOperation>(query.list());
    }

    /**
     * 根据菜单ID和菜单状态 返回菜单列表
     *
     * @param operationId
     * @param status
     * @return
     */
    @Override
    public Set<TdOperation> queryOperationTree(String operationId, String status) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select op from TdOperation op where 1=1 ");
        if(StringUtils.isNotBlank(operationId)){
            sb.append(" and op.parent = :operationId ");
        }
        if(StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        sb.append(" order by op.menuIndex ASC ");
        Query query=session.createQuery(sb.toString());
        if(StringUtils.isNotBlank(operationId)){
            query.setString("operationId",operationId);
        }
        if(StringUtils.isNotBlank(status)){
            query.setString("status",status);
        }
        return new HashSet<TdOperation>(query.list());
    }
    public String changeBeforeIndex(String operationId, String parentId, int menuIndex, int menuLevel, String oldParentId, int oldMenuIndex, int oldMenuLevel,int version) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        //首先更新原来位置同级节点的索引
        sb.append(" update from TdOperation op ");
        sb.append(" set op.menuIndex = op.menuIndex - 1");
        sb.append(" where op.parent = :oldParentId and op.operationId != :operationId and op.menuIndex > :oldMenuIndex and op.menuLevel = :oldMenuLevel");
        Query query=session.createQuery(sb.toString());
        query.setString("oldParentId",oldParentId);
        query.setString("operationId",operationId);
        query.setInteger("oldMenuIndex",oldMenuIndex);
        query.setInteger("oldMenuLevel",oldMenuLevel);
        query.executeUpdate();
        return null;
    }
    public String changeAfterIndex(String operationId, String parentId, int menuIndex, int menuLevel, String oldParentId, int oldMenuIndex, int oldMenuLevel,int version) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        //更新新位置的同级节点
        sb.append(" update from TdOperation op ");
        sb.append(" set op.menuIndex = op.menuIndex + 1");
        sb.append(" where op.parent = :parentId and op.operationId != :operationId and op.menuIndex >= :menuIndex and op.menuLevel = :menuLevel ");
        Query query=session.createQuery(sb.toString());
        query.setString("parentId",parentId);
        query.setString("operationId",operationId);
        query.setInteger("menuIndex",menuIndex);
        query.setInteger("menuLevel",menuLevel);
        query.executeUpdate();
        return null;
    }
    public String changeCurrentIndex(String operationId, String parentId, int menuIndex, int menuLevel, String oldParentId, int oldMenuIndex, int oldMenuLevel,int version) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        //首先更新原来位置同级节点的索引
        sb.append(" update from TdOperation op ");
        if (oldMenuIndex > menuIndex){
            sb.append(" set op.menuIndex = op.menuIndex + 1");
        }else{
            sb.append(" set op.menuIndex = op.menuIndex - 1");
        }
        sb.append(" where op.parent = :oldParentId and op.operationId != :operationId and op.menuLevel = :oldMenuLevel ");
        if (oldMenuIndex > menuIndex){
            sb.append(" and op.menuIndex < :oldMenuIndex and op.menuIndex >= :menuIndex ");
        }else{
            sb.append(" and op.menuIndex > :oldMenuIndex and op.menuIndex <= :menuIndex ");
        }
        Query query=session.createQuery(sb.toString());
        query.setString("oldParentId",oldParentId);
        query.setString("operationId",operationId);
        query.setInteger("oldMenuIndex",oldMenuIndex);
        query.setInteger("menuIndex",menuIndex);
        query.setInteger("oldMenuLevel",oldMenuLevel);
        query.executeUpdate();
        return null;
    }
    public String changeIndex(String operationId, String parentId, int menuIndex, int menuLevel, String oldParentId, int oldMenuIndex, int oldMenuLevel,int version) {
        TdOperation base = loadObject(TdOperation.class,parentId);
        TdOperation tdOperation =loadObject(TdOperation.class,operationId);
        tdOperation.setParent(base);
        tdOperation.setMenuIndex(menuIndex);
        tdOperation.setMenuLevel(menuLevel);
        tdOperation.setVersion(version);
        updateObject(tdOperation);
        return null;
    }

    /**
     * 查询菜单是否重复
     * @param operationId
     * @param menuName
     * @param status
     * @return
     */
    @Override
    public Integer queryOperationIsRepeat(String operationId,String menuName, String status) {
        Session session = getSession();
        StringBuffer sb = new StringBuffer();
        sb.append(" select count(op) from TdOperation op where 1=1 ");
        if (StringUtils.isNotBlank(menuName)){
            sb.append(" and op.menuName = :menuName ");
        }
        if (StringUtils.isNotBlank(status)){
            sb.append(" and op.status = :status ");
        }
        if (StringUtils.isNotBlank(operationId)){
            sb.append(" and op.operationId != :operationId ");
        }
        Query query=session.createQuery(sb.toString());
        if (StringUtils.isNotBlank(menuName)){
            query.setString("menuName", menuName);
        }
        if (StringUtils.isNotBlank(status)){
            query.setString("status", status);
        }
        if (StringUtils.isNotBlank(operationId)){
            query.setString("operationId", operationId);
        }
        return  ((Long)query.uniqueResult()).intValue();
    }
}
